package dsl.groovy.delegate

import dsl.groovy.conf.HttpConf
import dsl.groovy.utils.HttpClientUtils
import org.codehaus.groovy.runtime.DefaultGroovyMethods

import static dsl.groovy.utils.HttpClientUtils.sendHttpPostJson

/**
 * @author lesofn
 * @version 1.0 2021-03-02 11:59
 */
trait HttpDelegate {
    /**
     * http相关
     */
    String http(@DelegatesTo(HttpConf.class) Closure<HttpConf> closure) throws IOException {
        HttpConf conf = new HttpConf()
        DefaultGroovyMethods.with(conf, closure)
        println(conf.getUrl())
        // 执行调用
        def times = Integer.valueOf(conf.getTimes())
        println(times)
        StringBuilder sb = new StringBuilder()
        for (t in 1..times) {
            String a = sendHttpPostJson(conf.getUrl(), conf.getData())
            sb.append(a)
        }
        return sb.toString()
    }

}